package com.dragon.mmochat.message;

import java.util.ArrayList;
import java.util.List;

import com.dragon.mmochat.MmochatConstant;
import com.dragon.mmochat.model.object.MmochatCommonObject;
import com.dragon.mmochat.model.object.MmochatPackageGrid;
import com.skymobi.buffer.SkymobiBuffer;
import com.skymobi.message.SkymobiProtocolMessage;

public class Mmochat_5_1S extends SkymobiProtocolMessage {
	List<MmochatPackageGrid> grids = new ArrayList<MmochatPackageGrid>();

	public Mmochat_5_1S() {
		setMsgCode(MmochatConstant.MSGCODE_MMOCHAT_S_5_1);
	}

	@Override
	protected void encodeBody(SkymobiBuffer out) {
		if (grids == null || grids.size() == 0) {
			return;
		}
		out.putShort((short) grids.size());
		for (MmochatPackageGrid grid : grids) {
			out.putShort((short) grid.getGridIndex());
			out.putShort((short) grid.getGridAttrValue());
			MmochatCommonObject obj = grid.getObject();
			if (obj == null) {
				out.putShort((short) 0);
				out.putShort((short) 0);
				out.putShort((short) 0);
				out.putShort((short) 0);
			} else {
				// attr
				out.putShort((short) obj.getAttrValue());
				// bmpId
				out.putUnsignedShort(obj.getBmpId());
				// info
				out.putPrefixedString(obj.getInfo(), 2);
				// overlapNum
				out.putShort((short) obj.getOverlapNum());
			}
		}
	}

	public void setGrids(List<MmochatPackageGrid> grids) {
		this.grids = grids;
	}

	public void addGrid(MmochatPackageGrid grid) {
		if (grid != null) {
			grids.add(grid);
		}
	}

	public void addGrid(List<MmochatPackageGrid> gridList) {
		if (gridList != null) {
			grids.addAll(gridList);
		}
	}

	public boolean hasGrid() {
		return grids != null && grids.size() > 0;
	}
}